﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Common;

namespace Habito.Admini
{
	public partial class ProductDetail : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(Ticket.UserName) || Session["username"] == null)
			{
				Response.Redirect("Login.aspx");
			}
			ViewState["id"] = Request.QueryString["id"];
			ViewState["classID"] = Request.QueryString["classID"];
		}

		protected override void OnInit(EventArgs e)
		{
			if (Request.QueryString["GetProductByID"] == "1")
			{
				int id = int.Parse(Request.QueryString["id"].Replace("\'", ""));
				Ajax.BLL.Products product = new Ajax.BLL.Products();
				var pro = product.GetModel(id);
				Response.Write(JsonConvert.SerializeObject(pro));
				Response.End();
			}
			if (Request.QueryString["SaveProduct"] == "1")
			{
				int id = int.Parse(Request.QueryString["id"].Replace("\'", ""));
				int ClassID = int.Parse(Request.QueryString["ClassID"].Replace("\'", ""));
				int ShowIndex = int.Parse(Request.Params["ShowIndex"].Replace("\'", ""));
				int ShowIndexClass = int.Parse(Request.Params["ShowIndexClass"].Replace("\'", ""));
				string productName = Request.Params["productName"];
				string description = Request.Params["description"];
				string imagepath = Request.Params["imagePath"];

				Ajax.Model.Products products = new Ajax.Model.Products();
				products.ID = id;
				products.ClassID = ClassID;
				products.ProductName = productName;
				products.ShowInClass = ShowIndexClass;
				products.ShowInIndex = ShowIndex;
				products.ImagePath = imagepath;
				products.Description = description;

				Ajax.BLL.Products product = new Ajax.BLL.Products();
				if (products.ID == 0)
				{
					product.Add(products);
					var pro = product.GetModel(product.GetMaxId());
					Response.Write(JsonConvert.SerializeObject(pro));
				}
				else
				{
					product.Update(products);
					var pro = product.GetModel(products.ID);
					Response.Write(JsonConvert.SerializeObject(pro));
				}
				Response.End();
			}
			base.OnInit(e);
		}
	}
}